package com.ubao.weibo;


import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.WebView;
import android.widget.Toast;

import com.ubao.activity.ActivateActivity;
import com.ubao.activity.MyApp;
import com.ubao.activity.MyApp.GoBack;
import com.ubao.network.HttpsUtil;
import com.ubao.network.ResponseInterface;

public  class SinaWeiboOAuthActivity extends WeiboOAuthActivity  {
	//public WebView webview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//setContentView(R.layout.sina_oauth_webview);

		super.onCreate(savedInstanceState);
		webview.loadUrl(WeiboConstant.SINA_OAUTH);
		
	}
	String result;
	 public  void pageStarted(WebView view, String url, Bitmap favicon){
		 Log.e("onPageStarted", url + "开始加载界面。。。。。");
		 if(url.contains("error")){
			 finish();
			 return;
		 }
		if (url.startsWith(WeiboConstant.SINA_URL)) {
			// 取消授权后的界面
			view.cancelLongPress();
			view.stopLoading();
			

			// 获取Code
			Uri uri = Uri.parse(url);
			final String code = uri.getQueryParameter("code");
			Log.e("code", WeiboConstant.SINA_ACCESS_TOKEN + code);
			Log.e("uri", ""+uri);
			result = "";
			if (code != null) {
				new Thread(){
					@Override
					public void run() {
						result = HttpsUtil.HttpsPost(
								WeiboConstant.SINA_ACCESS_TOKEN + code, "");
						Log.e("Https地址", WeiboConstant.SINA_ACCESS_TOKEN + code);
						Log.e("登录请求结果", "" + result);
						handler.sendEmptyMessage(0);
					}
				}.start();
			}
			
		}
		
		}
	 
	 public void setGoBack() {
			Bundle gobackBundle = new Bundle();
			gobackBundle.putString("back2", this.getClass().getName());
		
			GoBack.goBackStack.push(gobackBundle);
		}
	 
	 Handler handler = new Handler(){
		 public void handleMessage(android.os.Message msg) {
			//finish();
			 switch(msg.what){
			 case 0:
				 if(result!=null){
					 if (result.startsWith("{\"access_token\":")) {
							int i = result.indexOf(":");
							int j = result.indexOf(",");
							final String token = result.substring(i + 2,
									j - 1);
							
							final String uid= "\"uid\":\"";
							new Thread(){
								public void run() {
									String nameResult = HttpsUtil.HttpsPost( WeiboConstant.getWeiboNameSina(getParam(uid,result,"\""),token),null);
									String name = getParam("\"screen_name\":\"",nameResult,"\"");	
									String uid  = getParam("\"idstr\":\"",nameResult,"\"");	
									String gender  = getParam("\"gender\":\"",nameResult,"\"");	
									Log.e("name+token", name+" + "+token + " result:" + nameResult);
									MyApp.weiboUser.bindSinaWeibo(token, 0,name,uid,gender); 
									handler.sendEmptyMessage(1);
								};
							}.start();
							
						}
					}else{
						Toast.makeText(SinaWeiboOAuthActivity.this, "绑定失败请再试一次",
								Toast.LENGTH_LONG).show();
					}
				 break;
			 case 1:
				 //GoBack.goBack(SinaWeiboOAuthActivity.this);
				 Intent intent = new Intent(SinaWeiboOAuthActivity.this,ActivateActivity.class);
				 startActivity(intent);
				 Toast.makeText(SinaWeiboOAuthActivity.this, "绑定成功",
						 Toast.LENGTH_LONG).show();
				 break;
			 }
		 };
	 };
	 
	@Override
	protected void httpCompleted(ResponseInterface resp) {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void getIntentData() {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void setIntentData() {
		// TODO Auto-generated method stub
		
	}
	 
	
}
